package com.jt.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JedisCluster jedisCluster;


    @RequestMapping("/findAll")
    public SysResult doFindAll(){
        List<User> userList=userService.findAll();
        return SysResult.success(userList);
    }

    /**
     *
     * url:http://sso.jt.com/user/check/admin111212/1?r=0.48285500575840223&callback=jsonp1616466482121&_=1616466551977
     * @return
     */

    @RequestMapping("/check/{param}/{type}")
    public JSONPObject checkUser(@PathVariable String param,
                                 @PathVariable Integer type,
                                 String callback){
        int count=userService.checkUser(param,type);
        boolean flag=count>0;
        return new JSONPObject(callback,SysResult.success(flag));
    }

    /**
     * 实现用户跨域数据的回显
     * url:http://sso.jt.com/user/query/749ec435-87ba-4707-9f01-d8d98513f5f9?callback=jsonp1616574112356&_=1616574112397
     * 参数
     */
    @RequestMapping("/query/{ticket}")
    public JSONPObject findUserByTicket(@PathVariable String ticket,String callback){
        String json = jedisCluster.get(ticket);
        if (json==null){
            return new JSONPObject(callback,SysResult.failed());
        }
        return new JSONPObject(callback,SysResult.success(json));
    }


}














